/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.CheckBox;
import com.sun.lwuit.ComboBox;
import com.sun.lwuit.Command;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BoxLayout;
import core.ScreenEngine;
import core.Settings;
import network.Connection;
import view.commands.BackCommand;

/**
 *
 * @author Kvest
 */
public class LoginEnterForm extends Form
{
    private static final int SAVE_ID = 1;
    
    private TextField login;
    private TextField password;
    private CheckBox saveLoginPass;
    
    private ComboBox updateType;
    
    private Command save;
    
    public LoginEnterForm()
    {
        super("settings");

        init();
    }

    private void init() 
    {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        
        //Создаем компоненты
        //Логин
        addComponent(new Label("login"));
        login = new TextField();
        login.setSelectCommandText("");
        login.setConstraint(TextArea.ANY);
        login.setInputModeOrder(new String[]{"Abc","ABC","abc","123"});
        addComponent(login);

        //Пароль
        addComponent(new Label("pass"));
        password = new TextField();
        password.setSelectCommandText("");
        password.setConstraint(TextArea.ANY);
        password.setInputModeOrder(new String[]{"Abc","ABC","abc","123"});
        addComponent(password);

        //Запоминать ли логин-пароль
        saveLoginPass = new CheckBox("remember_data");
        addComponent(saveLoginPass);
        
        //Компонент выбора типа файла при обновлении
        updateType = new ComboBox();
        updateType.addItem(Settings.JAD);
        updateType.addItem(Settings.JAR);
        addComponent(new Label("update_type"));
        addComponent(updateType);
        
        BackCommand cancel = new BackCommand();
        cancel.setCommandName("cancel");
        addCommand(cancel);
        
        save = new Command("ok",SAVE_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {
                //Сохраняем настроки
                saveSettings();

                //Возвращаемся на предыдущий экран
                ScreenEngine.getEngine().pop(true);

                //Если ввели данные для логина - начинаем соединение
                if (!Settings.getSettings().getLogin().equals("") && !Settings.getSettings().getPassword().equals(""))
                {
                    //Отображаем форму прогресса
                    ScreenEngine.getEngine().push(new ProgressForm(null));
                    
                    //Начинаем соединение
                    Connection.getConnection().start(Settings.getSettings().getHost(), Settings.getSettings().getSecondHost());
                }
                
                evt.consume();
            }
        };
        addCommand(save);
    }
    
    public void show()
    {
        //Устанавливаем текущие настройки
        setSettings();

        //Отображае форму
        super.show();
    }
    
    private void setSettings()
    {
        Settings settings = Settings.getSettings();
        
        login.setText(settings.getLogin());
        password.setText(settings.getPassword());
        saveLoginPass.setSelected(settings.isSaveLoginPass());
        
        if (settings.getUpdateType().equals(Settings.JAD))
        {
            updateType.setSelectedIndex(0);
        }
        else if (settings.getUpdateType().equals(Settings.JAR))
        {
            updateType.setSelectedIndex(1);
        }
    }
    
    private void saveSettings()
    {
        Settings settings = Settings.getSettings();
        
        settings.setLogin(login.getText());
        settings.setPassword(password.getText());
        settings.setSaveLoginPass(saveLoginPass.isSelected());
        
        switch(updateType.getSelectedIndex())
        {
            case 0 : settings.setUpdateType(Settings.JAD); break;
            case 1 : settings.setUpdateType(Settings.JAR); break;
        }
        
        //сохраняем
        settings.save();        
    }
}
